package com.zhyonk.controller;

import cn.hutool.core.lang.Console;
import com.zhyonk.config.propreties.ByteDanceProperties;
import com.zhyonk.domain.AjaxResult;
import com.zhyonk.domain.FeedBack;
import com.zhyonk.domain.ItemListInfo;
import com.zhyonk.mapper.FeedBackMapper;
import com.zhyonk.mapper.ItemListInfoMapper;
import com.zhyonk.mapper.ItemQueryInfoMapper;
import com.zhyonk.service.FaceService;
import com.zhyonk.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private IndexService indexService;

    @Autowired
    private ItemListInfoMapper itemListInfoMapper;

    @Autowired
    private ItemQueryInfoMapper itemQueryInfoMapper;

    @Autowired
    private FeedBackMapper feedBackMapper;

    @RequestMapping(value = "/getIndexBaseInfo",produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public AjaxResult getIndexBaseInfo(@RequestBody HashMap<String, Object> param) {
        return AjaxResult.success(indexService.getIndexBaseInfo());
    }

    @RequestMapping(value = "/getTopItemList",produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public AjaxResult getTopItemList(@RequestBody HashMap<String, Object> param) {
        Object itemIdObj = param.get("currItemId");
        Object categoryIdObj = param.get("categoryId");
        int itemId = -1;
        String msg = "";
        if (Objects.isNull(itemIdObj)){
            if (Objects.isNull(categoryIdObj)){
                return AjaxResult.error("缺乏参数");
            }
            return AjaxResult.success(indexService.getItemListByCategory(Integer.parseInt(categoryIdObj.toString())));
        }

        itemId = Integer.parseInt(itemIdObj.toString()); ;
        return AjaxResult.success(indexService.getTopItemList(itemId));
    }

    @RequestMapping(value = "/getItemDetailById",produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public AjaxResult getItemDetailById(@RequestBody HashMap<String, Object> param) {
        Object itemIdObj = param.get("queryId");
        if (Objects.isNull(itemIdObj)){
            return AjaxResult.error("没有queryId");
        }
        String itemId = itemIdObj.toString();
        return AjaxResult.success(itemQueryInfoMapper.selectById(itemId));
    }

    @RequestMapping(value = "/sendFeedBack",produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public AjaxResult sendFeedBack(@RequestBody HashMap<String, Object> param) {
        Object paramsObj = param.get("params");
        if (Objects.isNull(paramsObj)){
            return AjaxResult.error("无参数");
        }
        Map<String,Object> params = (Map<String, Object>) paramsObj;
        String score = params.get("score").toString();
        String content = params.get("content").toString();
        String contact = params.get("contact").toString();

        FeedBack feedBack = new FeedBack();
        feedBack.setCreateDate(new Date());
        feedBack.setContact(contact);
        feedBack.setContent(content);
        feedBack.setScore(Integer.parseInt(score));
        try{
            feedBackMapper.insert(feedBack);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
